<template>
  <div class="component-setting">
    <div class="component-mainHeader">
      <div class="top">
        <div class="topleft">
          <p>
            <span class="iconfont" style="color:#1abc9a;margin-right:3px;">&#xe607;</span>账户设置
          </p>
        </div>
        <div class="topright">
          <el-button type="info" @click="$router.go(0)">
            <span style="font-weight:700;margin-right:5px;" class="iconfont">&#xe662;</span>刷新
          </el-button>
        </div>
      </div>
    </div>

    <!--上传图像-->
    <div id="upload">
      <el-form ref="form" :model="form" label-width="80px">
        <!--<div class="setInfo">-->
        <!--<el-upload-->
        <!--class="avatar-uploader"-->
        <!--:data="exe_data"-->
        <!--action="/afiles/sysFile/uploadFile"-->
        <!--:show-file-list="false"-->
        <!--:on-success="handleAvatarSuccess"-->
        <!--:before-upload="beforeAvatarUpload">-->
        <!--<img v-if="imageUrl" :src="imageUrl" class="avatar">-->
        <!--<i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
        <!--</el-upload>-->
        <!--</div>-->
        <div class="box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号:" prop="userName">
              <el-input v-model="ruleForm.userName" disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="旧密码:" prop="userPwd">
              <el-input v-model="ruleForm.userPwd"></el-input>
            </el-form-item>
            <el-form-item label="新密码:" prop="newPwd">
              <el-input type="password" v-model="ruleForm.newPwd" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码:" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item style="padding-top:20px;">
              <el-button type="primary" @click="onSubmit('ruleForm')">提交</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  name: "setting",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      exe_data: { parentId: "1001", bizType: "project.complete.photo" },
      form: {
        name: "",
        filePath: "imgUrl",
        uid: "430293",
        bio: "欢迎识别绚丽多彩的世界！",
        background: "http://localhost:8090/594641ab-72c7-4681-815d-065807a894d1"
      },
      param: {}, //表单要提交的参数
      src: "", //展示图片的地址
      imageUrl: "",
      ruleForm: {
        userName: "",
        userPwd: "",
        newPwd: "",
        checkPass: ""
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 4, max: 16, message: "长度在 1 到 5 个字符", trigger: "blur" }
        ],
        userPwd: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
        newPwd: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.ruleForm.userName = localStorage.getItem("ms_username");
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let that = this,
            suCb = res => {
              console.log("提交成功信息：", res);
              that.$msg('修改成功')
              that.$router.push('/system')
            },
            erCb = res => {
              console.log("提交错误信息：", res);
              that.$msg('修改失败，旧密码错误！')
            },
            req = {
              url: "api/web/sysUser/updatePwd",
              methods: "post",
              data: {
                userId: localStorage.getItem("ms_userId"),
                userPwd: "",
                newPwd: ""
              },
              success: suCb,
              error: erCb
            };
          // 深拷贝后原值md5加密再赋值
          let pwds = that.deepClone(that.ruleForm);
          req.data.userPwd = that.$md5(pwds.userPwd);
          req.data.newPwd = that.$md5(pwds.newPwd);
          console.log("提交表单参数:", req.data);
          that.$https(req);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.setInfo {
  margin-top: 20px;
}
.setInfo .iconfont {
  font-size: 100px;
  color: #ccc;
}
.iconupImg span,
.iconupImg i {
  display: block;
}
.iconupImg span {
  font-size: 14px;
  color: #1abc9c;
}
.box {
  width: 500px;
  margin: 40px auto;
}

.box .el-button--primary {
  color: #fff;
  background-color: #1abc9c;
  border-color: #1abc9c;
}

.box .el-form-item {
  width: 450px;
}

.box .el-select {
  margin-left: -132px;
}
</style>
